package com.yipinhouse.reception.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.google.common.collect.ImmutableMap;
import com.yipinhouse.common.result.Result;
import com.yipinhouse.reception.client.UserWebQueryClient;

import com.yipinhouse.reception.client.param.LoginWebParam;
import com.yipinhouse.reception.common.utils.JwtUtil;
import com.yipinhouse.reception.common.utils.WebUtil;
import com.yipinhouse.user.client.UserQueryClient;
import com.yipinhouse.user.client.param.LoginParam;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController
@Slf4j
@RequestMapping("reception")
public class UserWebQueryApiImpl implements UserWebQueryClient {


    @Resource
    private UserQueryClient userQueryClient;

    /**
     * 用户登录
     * @return
     */
    @Override
    public Result login(String account,String password) {
        log.info("员工登录：{}", account);
        LoginParam loginParam = new LoginParam();
        loginParam.setAccount(account);
        loginParam.setPassword(password);
        Result result = userQueryClient.login(loginParam);
        Integer data = (Integer) result.getData();
        if (data != null) {
            HashMap<String, Object> map = new HashMap<>(ImmutableMap.of("userId", data, "account", account));
            String token = JwtUtil.createToken(map, 1440);
            return Result.getInstance(String.class).setMsg("登录成功").setCode(200).setData(token);
        }
        return Result.getInstance().setMsg("登录失败");
    }

    /**
     * 退出登录
     * @return
     */
    @Override
    public Result logout() {
        
        return null;
    }

    /**
     * 查看个人信息
     *
     * @return
     */
    @Override
    public Result checkInfo() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String userId1 = request.getHeader("userId");
        Integer userId = Integer.valueOf(userId1);
        Result result = userQueryClient.checkInfo(userId);
        return result;
    }
}
